package com.koron.sinoprobe.web.ledger.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.annocation.CasUser;
import com.koron.common.core.interceptor.OrderByInterceptor;
import com.koron.sinoprobe.web.ledger.bean.entity.SpReport;
import com.koron.sinoprobe.web.ledger.bean.query.SpReportQuery;
import com.koron.sinoprobe.web.ledger.bean.vo.SpReportVO;
import com.koron.sinoprobe.web.ledger.bean.vo.SpendVO;
import com.koron.sinoprobe.web.ledger.service.SpReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author twcao
 * @title: SpReportController
 * @projectName GDH_EAM_V2
 * @description: 项目汇报
 * @date 2021/12/211:10
 */
@Slf4j
@RestController
@RequestMapping("/sp/project/report")
@Api(tags = "专项台账-项目汇报")
public class SpReportController {

    @Autowired
    private SpReportService reportService;

    @GetMapping("/selectOne")
    @ApiOperation("项目汇报详情查询")
    @ApiImplicitParam(name = "id", value = "项目汇报id", required = true)
    public Response<SpReport> selectOne(@CasUser @ApiIgnore EamUser user, @RequestParam String id) {
        SpReport detail = ADOConnection.runTask(user.getCurrDs(), reportService, "queryById", SpReport.class, id);
        return Response.ok("查询成功", detail);
    }

    @GetMapping("/queryList")
    @ApiOperation("项目汇报分页查询")
    public Response<PageInfo<SpReportVO>> queryList(@CasUser @ApiIgnore EamUser user, SpReportQuery query) {
        OrderByInterceptor.setOrderBy(query.getOrders());
        PageHelper.startPage(query.getPage(), query.getPageSize());
        List<SpReportVO> list = ADOConnection.runTask(user.getCurrDs(), reportService, "queryList", List.class, query);
        return Response.ok("查询成功", new PageInfo<>(list));
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("新增或修改项目汇报")
    public Response<SpReport> saveOrUpdate(@CasUser @ApiIgnore EamUser user, @RequestBody SpReport detail) {
        detail = ADOConnection.runTask(user.getCurrDs(), reportService, "saveOrUpdate", SpReport.class, detail);
        return Response.ok("操作成功", detail);
    }

    @GetMapping("/deleteById")
    @ApiOperation("根据id删除项目汇报")
    @ApiImplicitParam(name = "id", value = "项目汇报id", required = true)
    public Response<String> deleteById(@CasUser @ApiIgnore EamUser user, @RequestParam String id) {
        ADOConnection.runTask(user.getCurrDs(), reportService, "deleteById", String.class, id);
        return Response.ok("删除成功", id);
    }

    @GetMapping("/getLatestReport")
    @ApiOperation("获取最新项目汇报")
    @ApiImplicitParam(name = "id", value = "项目汇报id", required = true)
    public Response<SpReportVO> getLatestReport(@CasUser @ApiIgnore EamUser user, @RequestParam String id) {
        SpReportVO report = ADOConnection.runTask(user.getCurrDs(), reportService, "getLatestReport", SpReportVO.class, id);
        return Response.ok("查询成功", report);
    }

    @GetMapping("/getReportSpend")
    @ApiOperation("获取年度支出列表（实时统计，只查不存）")
    @ApiImplicitParam(name = "id", value = "获取年度支出列表", required = true)
    public Response<List<SpendVO>> getReportSpend(@CasUser @ApiIgnore EamUser user, @RequestParam String id) {
        List<SpendVO> spends = ADOConnection.runTask(user.getCurrDs(), reportService, "getReportSpend", List.class, id);
        return Response.ok("查询成功", spends);
    }

    /*
     * 统计所有专项项目年度汇报情况sql
        select project.id, project.name, project.budget, sum(report.investment), DATE_FORMAT(report.create_time,'%Y') y
        from w_sp_project project
        left join w_sp_report report on report.project_id = project.id
        where project.delete_flag=0
        group by project.id, y
        order by project.create_time desc
     */
}
